package com.hua.emall.controller;


import com.hua.emall.dao.UserInfoDao;
import com.hua.emall.entity.User;
import com.hua.emall.untils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@Controller
public class UserController {


//    @RequestMapping("/hi")
//    public String home(){
//
//        List<User> list = userMapper.queryUserInfo(11);
//        String name = "springboot-thymeleaf";
//        for (User user : list) {
//            System.out.println(user.getCity());
//        }
//
//        return "hello";
//    }

    //    @RequestMapping("/hello")
//    @ResponseBody
//    public ModelAndView testThymeleaf(){
//        List<User> list = userMapper.queryUserInfo(11);
//        ModelAndView mad = new ModelAndView();
//        String name = "springboot-thymeleaf";
//       mad.getModel().put("name",name);
//        mad.setViewName("hello");
//        int a = 1/0;
//        System.out.println(a);
//        return mad;
//    }
    @RequestMapping("/signin")
    public String testSignin() {

        return "signin";
    }

    @Autowired
    private UserInfoDao userInfoDao;

    @RequestMapping(value = "/userLogin.htm", method = RequestMethod.POST)
    public String userLogin(@ModelAttribute User userLogin, HttpServletRequest request, HttpServletResponse response) {
        List<User> userList = userInfoDao.queryLoginInfo(userLogin.getUsername());
        Md5Util md5util = new Md5Util();
        String passWordMd5 = md5util.EncoderByMd5(userLogin.getPassword());
        HttpSession session = request.getSession();

            if (userLogin.getUsername().equals(userList.get(0).getUsername()) && passWordMd5.equals(userList.get(0).getPassword())) {
                session.setAttribute("username", userLogin.getUsername());
                session.setAttribute("password", passWordMd5);
                System.out.println("用户名正确");
                System.out.println("密码正确");
                return "hello";
            }else{
                if (userLogin.getUsername().equals(userList.get(0).getUsername())) {
                    System.out.println("用户名正确");

                    if (passWordMd5.equals(userList.get(0).getPassword())) {
                        System.out.println("密码正确");
                    } else {
                        System.out.println("密码不正确");
                        request.setAttribute("error", "密码不正确");
                        return "signin";
                    }
                } else {
                    System.out.println("用户不正确");
                    request.setAttribute("error", "用户不正确");
                    return "signin";
                }
            }


        return "signin";
        }
}
